var express = require('express');
var router = express.Router();

const models = require('../db/models');
const UserModel = models.getModel('user');

const ajax = require('../api/ajax'); // ajax
const sms_util = require('../util/sms_util'); // 短信
const md5 = require('blueimp-md5'); // md5
var svgCaptcha = require('svg-captcha'); // 验证码

const _filter = {'pwd': 0, '__v': 0}; // 查询过滤
const users = {};

// 密码登陆
router.post('/login_pwd', function (req, res) {
  const name = req.body.name;
  const pwd = md5(req.body.pwd);
  const captcha = req.body.captcha.toLowerCase();

  console.log('/login_pwd', name, pwd, captcha, req.session);

  if(captcha!==req.session.captcha) {
    return res.send({code: 1, msg: '验证码不正确'});
  }
  delete req.session.captcha;

  UserModel.findOne({name}, function (err, user) {
    if (user) {
      console.log('findUser', user);

      if (user.pwd !== pwd) {
        res.send({code: 1, msg: '用户名或密码不正确!'});
      } else {
        req.session.userid = user._id;

        res.send({code: 0, data: {
          _id: user._id,
          name: user.name,
          phone: user.phone
        }});
      }
    } else {
      const userModel = new UserModel({name, pwd});

      userModel.save(function (err, user) {
        // res.cookie('userid', user._id, {maxAge: 1000*60*60*24*7});
        req.session.userid = user._id;

        res.send({code: 0, data: {
          _id: user._id,
          name: user.name
        }});
      });
    }
  });
});
// 短信登陆
router.post('/login_sms', function (req, res, next) {
  var phone = req.body.phone;
  var code = req.body.code;

  console.log('/login_sms', phone, code);

  if (users[phone] != code) {
    res.send({code: 1, msg: '手机号或验证码不正确'});
    return;
  }
  delete users[phone];

  UserModel.findOne({phone}, function (err, user) {
    if (user) {
      req.session.userid = user._id;
      res.send({code: 0, data: user});
    } else {
      const userModel = new UserModel({phone});
      userModel.save(function (err, user) {
        req.session.userid = user._id
        res.send({code: 0, data: user})
      });
    }
  });
});
// 发送验证码
router.get('/captcha', function (req, res) {
  var captcha = svgCaptcha.create({
    ignoreChars: '0o1l',
    noise: 2,
    color: true
  });
  req.session.captcha = captcha.text.toLowerCase();
  console.log(req.session.captcha)
  /*res.type('svg');
  res.status(200).send(captcha.data);*/
  res.type('svg');
  res.send(captcha.data)
});
// 发送短信
router.get('/sendcode', function (req, res, next) {
  //1. 获取请求参数数据
  var phone = req.query.phone;
  //2. 处理数据
  //生成验证码(6位随机数)
  var code = sms_util.randomCode(6);
  //发送给指定的手机号
  console.log(`向${phone}发送验证码短信: ${code}`);
  sms_util.sendCode(phone, code, function (success) {//success表示是否成功
    if (success) {
      users[phone] = code
      console.log('保存验证码: ', phone, code)
      res.send({"code": 0})
    } else {
      //3. 返回响应数据
      res.send({"code": 1, msg: '短信验证码发送失败'})
    }
  })
});
// 退出登录
router.get('/logout', function (req, res) {
  // 清除浏览器保存的userid的cookie
  delete req.session.userid
  // 返回数据
  res.send({code: 0})
});
// 查询用户
router.get('/userinfo', function (req, res) {
  // 取出userid
  const userid = req.session.userid
  // 查询
  UserModel.findOne({_id: userid}, _filter, function (err, user) {
    // 如果没有, 返回错误提示
    if (!user) {
      // 清除浏览器保存的userid的cookie
      delete req.session.userid

      res.send({code: 1, msg: '请先登陆'})
    } else {
      // 如果有, 返回user
      res.send({code: 0, data: user})
    }
  })
});
// 获取首页分类列表
router.get('/index_category', function (req, res) {
  setTimeout(function () {
    const data = require('../data/index_category.json')
    res.send({code: 0, data})
  }, 300)
});
// 根据经纬度获取位置详情
router.get('/position/:geohash', function (req, res) {
  const {geohash} = req.params
  ajax(`http://cangdu.org:8001/v2/pois/${geohash}`)
    .then(data => {
      res.send({code: 0, data})
    })
});
// 根据经纬度获取商铺列表
// ?latitude=40.10038&longitude=116.36867
router.get('/shops', function (req, res) {
  const latitude = req.query.latitude
  const longitude = req.query.longitude

  setTimeout(function () {
    const data = require('../data/shops.json')
    res.send({code: 0, data})
  }, 300)
});
// 商铺搜索
router.get('/search_shops', function (req, res) {
  const {geohash, keyword} = req.query
  ajax('http://cangdu.org:8001/v4/restaurants', {
    'extras[]': 'restaurant_activity',
    geohash,
    keyword,
    type: 'search'
  }).then(data => {
    res.send({code: 0, data})
  })
});

module.exports = router;
